package cn.com.inossem.http;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import cn.com.inossem.network.NetworkConnect;

public class NetworkConnectPost {
	private final String TAG = NetworkConnectPost.class.getSimpleName();
	private Context context;
	private Handler handler;
	private int handlerid;
	private String body;

	public NetworkConnectPost(Context context, String url, Handler handler, int handlerid, String body) {
		this.context = context;
		this.handler = handler;
		this.handlerid = handlerid;
		this.body = body;
		getData(url);
	}

	private void getData(final String url) {
		final HttpPost httpPost = new HttpPost(url);
		try {
			StringEntity entity = new StringEntity(body, HTTP.UTF_8);
			entity.setContentType("application/json");
			httpPost.setEntity(entity);
			new Thread(new Runnable() {
				@Override
				public void run() {
					Message msg = new Message();
					try {
						final HttpResponse httpResponse = NetworkConnect.httpclient.execute(httpPost);
						String result = "";
						if (httpResponse.getStatusLine().getStatusCode() == 200) {
							HttpEntity entity = httpResponse.getEntity();
							try {
								result = EntityUtils.toString(entity);
							} catch (Exception e) {
								Log.e(TAG, "" + e.getMessage());
							}
							result.replaceAll("\r", "");
						} else {
							HttpEntity entity = httpResponse.getEntity();
							try {
								result = EntityUtils.toString(entity);
							} catch (Exception e) {
								Log.e(TAG, "" + e.getMessage());
							}

							result.replaceAll("\r", "");
							Log.i(TAG, result + "statusCode:" + httpResponse.getStatusLine().getStatusCode());
						}
						if (result != null && !result.isEmpty()) {
							try {
								JSONObject jsonObject = new JSONObject(result);
								int flagCode = jsonObject.getInt("code");
								switch (flagCode) {
									case 0:
										String message = jsonObject.getString("msg");
										msg.obj = message;
										msg.what = 99;
										handler.sendMessage(msg);
										break;
									case 1:
										msg.obj = result;
										msg.what = handlerid;
										handler.sendMessage(msg);
										break;
									case 2:
										msg.obj = "用户未登录";
										msg.what = 99;
										handler.sendMessage(msg);
										break;
									default:
										msg.obj = result;
										msg.what = handlerid;
										handler.sendMessage(msg);
										break;
								}
							} catch (JSONException e) {
								Log.e(TAG, "" + e.getMessage());
								msg.obj = "返回数据异常";
								msg.what = 99;
								handler.sendMessage(msg);
							}
						} else {
							msg.obj = "空字符串";
							msg.what = 99;
							handler.sendMessage(msg);
						}
					} catch (Exception e) {
						Log.e(TAG, "" + e.getMessage());
						msg.obj = "连接失败";
						msg.what = 99;
						handler.sendMessage(msg);
					}
				}
			}).start();
		} catch (UnsupportedEncodingException e1) {
			Log.e(TAG, "" + e1.getMessage());
		}
	}
}